package com.kool.kadmin.mqtt.service;

import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.PaginatorUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.mqtt.bean.MqttTopicDo;
import com.kool.kadmin.mqtt.dao.MqttTopicAuthDao;
import com.kool.kadmin.mqtt.dao.MqttTopicDao;
import com.kool.kadmin.mqtt.request.AddTopicReq;
import com.kool.kadmin.mqtt.request.DeleteTopicReq;
import com.kool.kadmin.mqtt.request.GetTopicPageReq;
import com.kool.kadmin.mqtt.request.UpdateTopicReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyu
 * @date 2021-04-21 14:52
 */
@Service
@Slf4j
public class TopicService {
    @Resource
    private MqttTopicDao topicDao;
    @Resource
    private MqttTopicAuthDao topicAuthDao;

    /**
     * 分页查询主题
     *
     * @param request
     * @return
     */
    public PageListResult getTopicPage(GetTopicPageReq request) {
        MqttTopicDo condition = new MqttTopicDo();

        int total = topicDao.countTotal(condition);
        int offset = PaginatorUtil.calculateOffset(request.getCurrentPage(), request.getPageSize());
        List<MqttTopicDo> list = topicDao.getPage(condition, offset, request.getPageSize());

        return ResultUtil.success(list, request.getCurrentPage(), request.getPageSize(), total);
    }

    /**
     * 新增主题
     *
     * @param request
     */
    public void addTopic(AddTopicReq request) {
        String topicName = request.getTopicName();
        //查询主题是否存在
        MqttTopicDo topicDo = topicDao.getByTopicName(topicName);
        if (topicDo != null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, String.format("主题【%s】已存在", topicName));
        }

        topicDo = new MqttTopicDo();
        topicDo.setTopicName(topicName)
                .setTopicDesc(request.getTopicDesc());
        //插入
        topicDao.insert(topicDo);
    }

    /**
     * 更新主题
     *
     * @param request
     */
    public void updateTopic(UpdateTopicReq request) {
        long id = request.getId();
        //查询主题
        MqttTopicDo topicDo = topicDao.getById(id);
        if (topicDo == null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "主题不存在");
        }
        topicDo.setTopicName(request.getTopicName())
                .setTopicDesc(request.getTopicDesc());
        topicDao.updateById(topicDo);
    }

    /**
     * 删除主题
     *
     * @param request
     */
    @Transactional
    public void deleteTopic(DeleteTopicReq request) {
        long id = request.getId();
        //查询主题
        MqttTopicDo topicDo = topicDao.getById(id);
        if (topicDo == null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "主题不存在");
        }

        //删除用户主题权限中主题是该主题的权限
        int deleted = topicAuthDao.deleteByTopicName(topicDo.getTopicName());

        //删除主题
        topicDao.deleteById(id);
        log.debug("共删除【{}】条主题权限", deleted);
    }
}
